package com.example.lending.View.Adapter;

import android.content.Context;
import android.icu.util.ValueIterator;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProviders;
import androidx.recyclerview.widget.RecyclerView;

import com.example.lending.Model.Borrow;
import com.example.lending.R;
import com.example.lending.ViewModel.BorrowViewModel;

import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

public class BookRecordAdapter extends RecyclerView.Adapter {
    private Context context;
    private List<Borrow>borrows;


    public BookRecordAdapter(Context context, List<Borrow> borrows) {
        this.context = context;
        this.borrows = borrows;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if(holder instanceof MyViewHolder){
            Borrow book = borrows.get(position);
            ((MyViewHolder) holder).tvName.setText("书名："+book.getName());
            ((MyViewHolder) holder).tvDuration.setText("剩余还书时间："+book.getDuration()+"天");
            ((MyViewHolder) holder).tvTime.setText("借书时间："+book.getTime());
            ((MyViewHolder) holder).tvState.setText("借书地点:"+book.getState());
            //((MyViewHolder) holder).tvUser.setText("借书人："+ DbUtil.getUserName(context,book.getUserid()));
            ((MyViewHolder) holder).tvUser.setText("借书人："+book.getUserid());
            //BorrowViewModel borrowViewModel= ViewModelProviders.of(context).get(BorrowViewModel.class);
            //这里还没写完

        }
    }

    @Override
    public int getItemCount() {
        if(borrows==null)
            return 0;
        return borrows.size();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.item_borrow,parent,false);
        return new BorrowAdapter.MyViewHolder(view);
    }


    class MyViewHolder extends RecyclerView.ViewHolder{
        TextView tvName;
        TextView tvDuration;
        TextView tvState;
        TextView tvTime;
        TextView tvUser;

        public MyViewHolder(View itemView) {
            super(itemView);
            tvName = itemView.findViewById(R.id.tv_namebook2);
            tvDuration = itemView.findViewById(R.id.tv_sexbook2);
            tvState = itemView.findViewById(R.id.tv_majorbook2);
            tvTime = itemView.findViewById(R.id.tv_classbook2);
            tvUser = itemView.findViewById(R.id.tv_telbook2);
        }
    }
}
